﻿using System;
using System.Windows.Input;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Keyboard = Microsoft.Xna.Framework.Input.Keyboard;

namespace Silverlight3dApp.Learning.Lesson05
{
    public class UserControlledSprite : Sprite
    {
        private MouseState _preMouseState;

        public UserControlledSprite(Texture2D texture, Vector2 position, Vector2 frameSize, int collisionOffset, Vector2 currentFrame, Vector2 sheetSize, Vector2 speed) : base(texture, position, frameSize, collisionOffset, currentFrame, sheetSize, speed, null, 0)
        {
        }

        public UserControlledSprite(Texture2D texture, Vector2 position, Vector2 frameSize, int collisionOffset, Vector2 currentFrame, Vector2 sheetSize, Vector2 speed, int millisecondsPerFrame) : base(texture, position, frameSize, collisionOffset, currentFrame, sheetSize, speed, null, millisecondsPerFrame, 0)
        {
        }

        private int _livesRemaining = 3;

        public int LivesRemaining
        {
            get
            {
                return _livesRemaining;
            }
            set
            {
                _livesRemaining = value;
                if (_livesRemaining == 0)
                {
                    OnDie();
                }
            }
        }

        public float LivesScale = 0.5f;

        public event EventHandler<EventArgs> Die;

        protected void OnDie()
        {
            var die = Die;
            if (die != null)
            {
                die(this, EventArgs.Empty);
            }
        }

        public override Vector2 Direction
        {
            get
            {
                var inputDirection = Vector2.Zero;
                var keyboardState = Keyboard.GetState();
                if (keyboardState.IsKeyDown(Key.Left))
                {
                    inputDirection.X -= 1;
                }
                if (keyboardState.IsKeyDown(Key.Right))
                {
                    inputDirection.X += 1;
                }
                if (keyboardState.IsKeyDown(Key.Up))
                {
                    inputDirection.Y -= 1;
                }
                if (keyboardState.IsKeyDown(Key.Down))
                {
                    inputDirection.Y += 1;
                }
                if (keyboardState.IsKeyDown(Key.A))
                {
                    return inputDirection * _Speed * 2;
                }
                return inputDirection * _Speed;
            }
        }

        public override void Update(GameTime gameTime, Rectangle clientBounds)
        {
            _Position += Direction;

            var mouseState = Mouse.GetState();
            if (mouseState.X != _preMouseState.X || mouseState.Y != _preMouseState.Y)
            {
                _Position = new Vector2(mouseState.X, mouseState.Y);
                _preMouseState = mouseState;
            }

            if (_Position.X < 0)
            {
                _Position.X = 0;
            }
            if (_Position.Y < 0)
            {
                _Position.Y = 0;
            }
            var maxWidth = clientBounds.Width - _FrameSize.X;
            if (_Position.X > maxWidth)
            {
                _Position.X = maxWidth;
            }
            var maxHeight = clientBounds.Height - _FrameSize.Y;
            if (_Position.Y > maxHeight)
            {
                _Position.Y = maxHeight;
            }

            base.Update(gameTime, clientBounds);
        }

        public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            base.Draw(gameTime, spriteBatch);
            for (int index = 0; index < _livesRemaining; index++)
            {
                spriteBatch.Draw(_Texture, new Vector2(10 + index * 40, 35), new Rectangle((int)(_CurrentFrame.X * _FrameSize.X), (int)(_CurrentFrame.Y * _FrameSize.Y), (int)_FrameSize.X, (int)_FrameSize.Y), Color.White, 0, Vector2.Zero, LivesScale, SpriteEffects.None, 0);
            }
        }
    }
}